package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.resp.R;
import com.tcm.service.LearningTimeService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 学生学习时长控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/23 18:14
 */
@RestController
@RequestMapping("/stu/learningTime")
public class StuLearningTimeController {

    @Autowired
    private LearningTimeService learningTimeService;

    /**
     * 保存学习时长
     * @param param 学习时长参数
     * @param request 请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R saveStuLearningTime(@RequestBody Map<String, Integer> param, HttpServletRequest request){
        Integer seconds = param.get("seconds");
        if (seconds == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return learningTimeService.saveStuLearningTime(seconds, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 获取一周内学习时长统计
     * @param request 请求对象
     * @return 一周内学习时长统计
     */
    @RequestMapping(value = "/week", method = RequestMethod.GET)
    public R getLearningTimeOfWeek(HttpServletRequest request){
        return R.success(learningTimeService.getLearningTimeOfWeek(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 获取一月内学习时长统计
     * @param request 请求对象
     * @return 一周内学习时长统计
     */
    @RequestMapping(value = "/month", method = RequestMethod.GET)
    public R getLearningTimeOfMonth(HttpServletRequest request){
        return R.success(learningTimeService.getLearningTimeOfMonth(AccessTokenUtil.getUserId(request)));
    }

}
